/**
 * DataViewController
 * 
 * Version 1.0
 *
 * Nov 29th, 2009
 * 
 * Copyright (c) 2009 Duc Nguyen
 */
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
 * each data view controller is tied to one instance of data view. 
 * @author Duc Nguyen
 *
 */


public class DataViewController extends BrickController{
	/** the data view this controller belongs to */
	private DataView dataView;
	
	/** 
	 * Constructor
	 * @param frame		the brick frame this controller refers to
	 * @param dataView	the view this controller refers to
	 */
	DataViewController(BrickFrame frame, DataView dataView){
		super(frame);
		this.dataView = dataView;
	}
	
	/**
	 * gets changed field in its data view, and makes changes to the model
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		try{
			
			if(arg0.getSource() == dataView.getHeightField()){
				editHeight();
			}
			if(arg0.getSource() == dataView.getWidthField()){
				editWidth();
			}
			if(arg0.getSource() == dataView.getLengthField()){
				editLength();
			}

		//command-specific exception
		}catch(CommandException b){
			JOptionPane.showMessageDialog(new JFrame(), 
					"There was an input Error. " + b.getMessage()
					);
			resetFields();
		//case where nothing is entered in input
		}catch(NullPointerException n){
			resetFields();
		//case where the input is not a number
		}catch(NumberFormatException f){
			JOptionPane.showMessageDialog(new JFrame(), 
					"There was an input Error. Input was not a number."); 
			resetFields();
		}
	}
	
	/**
	 * get the DataView to reset its fields to the model's attributes on error input
	 */
	private void resetFields(){
		dataView.getLengthField().setText("" + getModel().getLength());
		dataView.getWidthField().setText("" + getModel().getWidth());
		dataView.getHeightField().setText("" + getModel().getHeight());
	}
	
	/**
	 * Edit height of the brick logic
	 * @throws CommandException
	 * @throws NullPointerException		if input is empty or window is canceled/closed
	 * @throws NumberFormatException	if input is not a number
	 */
	private void editHeight()throws CommandException, NullPointerException, NumberFormatException{
		String str = "";
		double oldHeight = this.getModel().getHeight();
		double newHeight;
		str = dataView.getHeightField().getText();
		
		newHeight = Double.parseDouble(str);
		//set Height
		if(newHeight != oldHeight){
			this.getCommandProcessor().execute(new SetHeight(this.getModel(), newHeight));
		}else{
			resetFields();
		}
	}
	
	/**
	 * Edit width of the brick logic
	 * @throws CommandException
	 * @throws NullPointerException		if input is empty or window is canceled/closed
	 * @throws NumberFormatException	if input is not a number
	 */
	private void editWidth()throws CommandException, NullPointerException, NumberFormatException{
		String str = "";
		double oldWidth = this.getModel().getWidth();
		double newWidth;
		str = dataView.getWidthField().getText();
		
		newWidth = Double.parseDouble(str);
		
		//set width
		if(newWidth != oldWidth){
			this.getCommandProcessor().execute(new SetWidth(this.getModel(), newWidth));
		}else{
			resetFields();
		}
	}
	
	/**
	 * Edit length of the brick logic
	 * @throws CommandException
	 * @throws NullPointerException		if input is empty or window is canceled/closed
	 * @throws NumberFormatException	if input is not a number
	 */
	private void editLength()throws CommandException, NullPointerException, NumberFormatException{
		String str = "";
		double oldLength = this.getModel().getLength();
		double newLength;
		str = dataView.getLengthField().getText();
		
		newLength = Double.parseDouble(str);
		
		//set length
		if(newLength != oldLength){
			this.getCommandProcessor().execute(new SetLength(this.getModel(), newLength));
		}else{
			resetFields();
		}
	}

}
